import { createRouter, createWebHistory } from 'vue-router';
import Layout from "../components/Layout/index.vue";
const routes = [
    { path: '/', redirect: '/main' },
    { path: "/login", name: "Login", component: () => import('../views/Login.vue') },
    {
        path: "/main", component: Layout, redirect: "/home", children: [
            { path: "/home", name: "Home", component: () => import("../views/Home.vue") },
        ]
    },
    {
        path: "/lybs", name: "留言板管理", component: Layout, children: [
            { path: "/lybs-list", name: "留言板列表", component: () => import("../views/LybsList.vue") }
        ]
    },
    {
        path: "/user", name: "用户管理", component: Layout, children: [
            { path: "/user-list", name: "用户列表", component: () => import("../views/UserList.vue") },
            { path: "/user-setting", name: "用户设置", component: () => import("../views/UserSetting.vue") },
        ],
    },
    { path: "/:pathMatch(.*)*", name: "404页面", component: () => import("../views/NotFound.vue") }
];
const router = createRouter({
    // 使用createWebHashHistory，生成的前端url中会携带#，如：http://192.168.18.200:8008/#/login
    // 使用createWebHistory，生成的url为http://192.168.18.200:8008/login
    history: createWebHistory(),
    routes,
});
// 路由导航守卫
router.beforeEach((to, from, next) => {
    // to:将要访问的路径
    // from:表示从那个路径跳转过来的
    // next:是一个函数，放行，断续访问
    if (to.path === "/login") return next();
    // 获取token
    const tokenStr = window.sessionStorage.getItem("token");
    if (!tokenStr) return next("/login");
    next();
})
export default router;